/**
 * @param {number[]} nums
 * @return {number}
 */
var maxProduct = function (nums) {
  let min = nums[0],
    max = nums[0],
    result = nums[0];
  for (let i = 1; i < nums.length; i++) {
    const tempMin = min,
      tempMax = max;
    min = Math.min(nums[i] * tempMin, nums[i] * tempMax, nums[i]);
    max = Math.max(nums[i] * tempMax, nums[i] * tempMin, nums[i]);
    result = Math.max(result, max);
  }
  return result;
};
let nums = [2, 3, -2, 4];
console.log(maxProduct(nums));
nums = [-2, 0, -1];
console.log(maxProduct(nums));
